package com.fh.controller.system.users;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.Menu;
import com.fh.entity.system.Role;
import com.fh.service.system.company.CompanyService;
import com.fh.service.system.contact.ContactService;
import com.fh.service.system.role.RoleService;
import com.fh.service.system.users.UsersService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.MD5;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;
 
@Controller
@RequestMapping(value="/users")
public class UsersController extends BaseController {
	
	String menuUrl = "users/listUsers.do"; //菜单地址(权限用)
	@Resource(name="usersService")
	private UsersService usersService;
	@Resource(name="roleService")
	private RoleService roleService;
	
	
	
	/**
	 * 保存
	 */
	@RequestMapping(value="/saveUsers")
	public ModelAndView saveUsers(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		usersService.saveUsers(pd);
		mv.addObject("msg","success");
		//mv.addObject("msg","failed");
		
		mv.setViewName("save_result");
		
		return mv;
	}
	/**
	 * 去新增用户页面
	 */
	@RequestMapping(value="/goAddUsers")
	public ModelAndView goAddUsers(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("system/users/users_edit");
			pd.getString("id");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/editUsers")
	public ModelAndView editUsers(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("user_id") != null && !"".equals(pd.getString("user_id"))){
			
			pd.put("user_id", pd.getString("user_id"));
		}
		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){usersService.editUsers(pd);}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}

	@RequestMapping(value="/toEditUsers")
	public ModelAndView toEdit(String user_id)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			pd.put("user_id",user_id);
			pd = usersService.findUsersById(pd);
			
			mv.addObject("pd", pd);
			mv.setViewName("system/users/users_edit");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 显示列表
	 */
	@RequestMapping(value="/listUsers")
	public ModelAndView listUsers(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	usersList = usersService.listAllUsers(page);			//列出用户列表
			List<Role> roleList = roleService.listAllappERRoles();					//列出所有会员二级角色
			
			mv.setViewName("system/users/users_list");
			mv.addObject("usersList", usersList);
			mv.addObject("roleList", roleList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
		return mv;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value="/deleteUsers")
	public ModelAndView deleteUsers(PrintWriter out){
		PageData pd = new PageData();
		ModelAndView mv = this.getModelAndView();
		try{
			pd = this.getPageData();
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){usersService.deleteUsers(pd);}
			mv.setViewName("system/users/users_list");
			 
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
		
	}
	
	/**
	 * 批量删除
	 */
	/*@RequestMapping(value="/deleteAllContact")
	@ResponseBody
	public Object deleteAllContact() {
		PageData pd = new PageData();
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String CONTACT_IDS = pd.getString("CONTACT_IDS");
			
			if(null != CONTACT_IDS && !"".equals(CONTACT_IDS)){
				String ArrayCONTACT_IDS[] = CONTACT_IDS.split(",");
				if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){contactService.deleteAllContact(ArrayCONTACT_IDS);}
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	*/
	
	
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
